home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 1146 / 1146.xpi / chrome / screengrab.jar / content / EmbeddedCapture.js < prev    next >
Text File  |  2009-03-09  |  3KB  |  83 lines

  1.  
  2. screengrab.EmbeddedCapture = function(htmlDoc, captureBox, context) {
  3.     this.context = context;
  4.     this.captureBox = captureBox;
  5.     // only get the embeds that are in the area to capture
  6.     var toCopy = htmlDoc.getAllEmbeddedDimensions()
  7.                    .filter(function(element) {
  8.                           return element.intersects(captureBox)
  9.                     });
  10.     
  11.     var regions = new Array();
  12.     var viewportDimensions = screengrab.Browser.viewportAbsoluteDimensions();
  13.     toCopy.forEach(function(element) {
  14.         // only want to capture the bit that's in the target area
  15.         var intersection = element.intersection(captureBox);
  16.         
  17.         // split the embeds so that they will fit into the window
  18.         var split = intersection.toBoxesSmallerThan(viewportDimensions.width, viewportDimensions.height);
  19.         sg.debug("Split... " + split);
  20.         regions = regions.concat(split);
  21.     });
  22.     this.regions = regions;
  23. }
  24. screengrab.EmbeddedCapture.prototype = {
  25.     capture: function(onFinish, htmlWindow) {
  26.         if (this.regions.length == 0 || !screengrab.Java.isAvailable()) {
  27.             onFinish();
  28.             return;
  29.         }
  30.         var viewportDimensions = screengrab.Browser.viewportAbsoluteDimensions();
  31.         var context = this.context;
  32.         var regions = this.regions;
  33.         var img = new Image();
  34.         var box = new sg.Box(0, 0);
  35.         var win = htmlWindow.htmlWin;
  36.         var origScrollX = win.scrollX;
  37.         var origScrollY = win.scrollY;
  38.         var captureBox = this.captureBox;
  39.         
  40.         var nextImage = function() {
  41.             sg.debug("Number of regions to grab " + regions.length);
  42.             var element = regions.pop();
  43.             box.x = element.x;
  44.             box.y = element.y;
  45.             box.width = element.width;
  46.             box.height = element.height;
  47.             var visibleRegion = new screengrab.Box(win.scrollX, win.scrollY, viewportDimensions.width, viewportDimensions.height);
  48.             sg.debug("Visible " + visibleRegion + " To capture " + element);
  49.             var doJavaCopy = function() {
  50.                 var copy = element.offsetCopy(viewportDimensions.x - win.scrollX, viewportDimensions.y - win.scrollY);
  51.                 var dataUrl = screengrab.Java.capture(copy);
  52.                 sg.debug("Setting url");
  53.                 img.src = dataUrl;
  54.             }
  55.             
  56.             if (!visibleRegion.contains(box)) {
  57.                 // scroll to region, snap it and save it
  58.                 sg.debug("Scrolling to [" + box.x + "," + box.y + "]");
  59.                 win.scrollTo(box.x, box.y);
  60.                 setTimeout(doJavaCopy, sg.prefs.javaScrollWaitTime());
  61.             } else {
  62.                 // slight delay to allow everything to get into place
  63.                 setTimeout(doJavaCopy, 10);
  64.             }
  65.         }
  66.         
  67.         img.onload = function() {
  68.             sg.debug("onLoad hit");
  69.             context.save();
  70.             context.drawImage(img, box.x - captureBox.x, box.y - captureBox.y);
  71.             context.save();
  72.             if (regions.length != 0) {
  73.                 sg.debug("Next");
  74.                 nextImage();
  75.             } else {
  76.                 sg.debug("Finishing");
  77.                 win.scrollTo(origScrollX, origScrollY);
  78.                 onFinish();
  79.             }
  80.         }
  81.         nextImage();
  82.     }
  83. }